#!/usr/bin/env python3
# -*- coding: utf-8 -*-


'''
双色球爬虫
http://kaijiang.zhcw.com/zhcw/html/ssq/list.html
'''

import requests;
from bs4 import BeautifulSoup;
import re;
from entity.Double_Color_Ball import *;
from datetime import datetime;
import time;

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62",
    "Cookie":""
}
headers_sfac={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62",
    "Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwicm9sZSI6IkFkbWluIiwidXNlckltYWdlIjoiaHR0cDovL3d3dy5zZmFjLnh5ejo4MDAwL2ltYWdlcy9wcm9maWxlLzE2ODg3MTE5MjI0NDkuanBnIiwiaWQiOjEsImV4cCI6MTY5MzM2NzMxNiwidXNlck5hbWUiOiJhZG1pbiIsImlhdCI6MTY5MzI4MDkxNn0.ND2M1Iu19w2x0V_-TLTeBjF1jk80HQYdBAkT2fsEMeM"
}

# 获取单页数据
def get_dcb_page_data(url):
    print("单页数据：%s" % url);

    # 向目标网址发送请求，获得响应
    r = requests.get(url, headers=headers);
    # 根据响应状态做处理
    if r.status_code == 200:
        # 设置响应编码
        r.encoding = r.apparent_encoding;
        # 打印响应文本内容
        # print(r.text);

        # 解析响应的内容
        # 构造 bs 对象
        bs = BeautifulSoup(markup=r.text, features="html.parser");
        # 获取所有 tr 标签
        tr_list = bs.find_all(name="tr");
        for index, tr in enumerate(tr_list):
            if index == 0 or index == 1 or index == (len(tr_list) - 1):
                continue;
            # print(tr);

            # 构造 DCB 对象
            dcb = Double_Color_Ball(
                issur_code=re.findall('<td align="center">(.*?)</td>', str(tr))[1],
                occur_date=re.findall('<td align="center">(.*?)</td>', str(tr))[0],
                red_ball=" ".join(re.findall('<em class="rr">(.*?)</em>', str(tr))),
                blue_ball=re.findall('<em>(.*?)</em>', str(tr))[0],
                create_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                update_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            );
            # print(dcb.__dict__);
            # 插入数据库
            insert_(dcb, "issur_code");

# 获取所有数据
def get_dcb_all_data(page_size=153):
    # 生成链接列表
    urls = list("http://kaijiang.zhcw.com/zhcw/html/ssq/list_%d.html" % page for page in range(2, page_size + 1));
    urls.insert(0, "http://kaijiang.zhcw.com/zhcw/html/ssq/list.html");

    for url in urls:
        get_dcb_page_data(url);
        time.sleep(5);

# 获得 sfac 单页数据
def get_dcb_page_data_sfac(url, currentPage=1, pageSize=10):
    print("单页数据：%s" % url);
    search = {"currentPage":currentPage,"pageSize":pageSize,"keyword":"","sort":"issue_no","direction":"desc"};
    # 向目标网址发送请求，获得响应
    r = requests.post(url, json=search, headers=headers_sfac);
    # 根据响应状态做处理
    if r.status_code == 200:
        # 设置响应编码
        r.encoding = r.apparent_encoding;
        print(r.json().get("total"));

        for item in r.json().get("list", []):
            # 构造 DCB 对象
            dcb = Double_Color_Ball(
                issur_code=item.get("issueNo"),
                occur_date=item.get("awardDate"),
                red_ball=item.get("redBall"),
                blue_ball=item.get("blueBall"),
                create_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                update_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            );
            print(dcb.__dict__);
            insert_(dcb, "issur_code");

# 获取 sfac 所有数据
def get_dcb_all_data_sfac(page_size=31):
    url_sfac = "http://538b537e25.zicp.vip:13595/api/economy/bicolorSpheres";
    page_size = 100;
    for page in range(1, page_size + 1):
        get_dcb_page_data_sfac(url_sfac, page, page_size);
        time.sleep(3);

if __name__ == '__main__':
    url = "http://kaijiang.zhcw.com/zhcw/html/ssq/list.html";
    get_dcb_page_data(url);
    get_dcb_all_data();
    # url_sfac= "http://538b537e25.zicp.vip:13595/api/economy/bicolorSpheres";
    # get_dcb_page_data_sfac(url_sfac);
    # get_dcb_all_data_sfac();